﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Application.AppUserManage;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;

namespace Zocono.WCS.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly ILogUtil _logger;
        private readonly UserInfoApp _userApp;
        public UserController(ILogUtil logger, UserInfoApp userApp)
        {
            _logger = logger;
            _userApp = userApp;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<HeaderResult<UserInfoApiDto>> LoginAsync(string userName, string password)
        {
            try
            {
                var user = await _userApp.LoginAsync(userName, password);
                HeaderResult<UserInfoApiDto> headerResult = new HeaderResult<UserInfoApiDto>
                {
                    IsSucceed = user.UserID != 0,
                    Result = user
                };
                return headerResult;
            }
            catch (Exception ex)
            {
                HeaderResult<UserInfoApiDto> headerResult = new HeaderResult<UserInfoApiDto>
                {
                    IsSucceed = false,
                    Message = ex.Message
                };
                _logger.Error(ex.ToString());
                return headerResult;
            }
        }

        [HttpPost]
        public async Task<HeaderResult<List<string>>> InitPasswordAsync(List<string> userNames)
        {
            bool ret = await _userApp.InitPasswordAsync(userNames);
            HeaderResult<List<string>> headerResult = new HeaderResult<List<string>>();
            if (ret)
            {
                headerResult.IsSucceed = true;
                headerResult.Message = "密码重置成功";
            }
            else
            {
                headerResult.IsSucceed = false;
                headerResult.Message = "密码重置失败";
            }
            return headerResult;
        }


        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="newPassword"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<HeaderResult<bool>> UpdatePasswordAsync(long ID, string newPassword)
        {
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            try
            {
                bool ret = await _userApp.UpdatePasswordAsync(ID, newPassword);

                if (ret)
                {
                    headerResult.IsSucceed = true;
                    headerResult.Message = "密码修改成功";
                }
                else
                {
                    headerResult.IsSucceed = false;
                    headerResult.Message = "密码修改失败";
                }
                return headerResult;
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.Message;
                return headerResult;
            }
        }


        [HttpPost]
        public async Task<HeaderResult<bool>> UpdateMenuInfoAsync(MenuInfoDto menuInfoDto)
        {
            var res = await _userApp.UpdateMenuInfoAsync(menuInfoDto);

            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            try
            {
                headerResult.IsSucceed = res;
                if (res)
                {
                    headerResult.Message = "更新成功";
                }
                else
                {
                    headerResult.Message = "更新失败";
                }
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message=ex.Message;
                throw;
            }
            return headerResult;
        }


        /// <summary>
        /// 添加用户和权限
        /// </summary>
        /// <param name="userMangerDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<HeaderResult<bool>> InsertUserAndRightAsync(List<UserMangerDto> userMangerDto)
        {
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            try
            {
                bool result = await _userApp.InsertUserRoleAsync(userMangerDto);
                if (result)
                {
                    headerResult.IsSucceed = true;
                    headerResult.Message = "用户添加成功";
                }
                else
                {
                    headerResult.IsSucceed = false;
                    headerResult.Message = "用户添加失败";
                }
                return headerResult;
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.Message;
                return headerResult;
            }
        }

        /// <summary>
        /// 获取菜单ByID
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<HeaderResult<MenuInfoDto>> GetMenuByIDAsync(long menuID)
        {
            HeaderResult<MenuInfoDto> headerResult = new HeaderResult<MenuInfoDto>();

            try
            {
                var res = await _userApp.GetMenuByIDAsync(menuID);
                if (res != null)
                {
                    headerResult.IsSucceed = true;
                    headerResult.Result = res;
                    headerResult.Message = "菜单添加成功";
                }
                else
                {
                    headerResult.IsSucceed = false;
                    headerResult.Message = "菜单添加失败";
                }
                return headerResult;
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.Message;
                return headerResult;
            }
        }


        /// <summary>
        /// 获取用户ByID
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<HeaderResult<UserInfoApiDto>> GetUserByIDAsync(long userID)
        {
            HeaderResult<UserInfoApiDto> headerResult = new HeaderResult<UserInfoApiDto>();

            try
            {
                var res = await _userApp.GetUserByIDAsync(userID);
                if (res != null)
                {
                    headerResult.IsSucceed = true;
                    headerResult.Result = res;
                    headerResult.Message = "菜单添加成功";
                }
                else
                {
                    headerResult.IsSucceed = false;
                    headerResult.Message = "菜单添加失败";
                }
                return headerResult;
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.Message;
                return headerResult;
            }

        }


        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="menuInfoDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<HeaderResult<bool>> AddMenuAsync(MenuInfoDto menuInfoDto)
        {
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            try
            {
                var res = await _userApp.AddMenuAsync(menuInfoDto);
                if (res)
                {
                    headerResult.IsSucceed = true;
                    headerResult.Message = "菜单添加成功";
                }
                else
                {
                    headerResult.IsSucceed = false;
                    headerResult.Message = "菜单添加失败";
                }
                return headerResult;
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.Message;
                return headerResult;
            }
        }



        ///// <summary>
        ///// 获取用户权限
        ///// </summary>
        ///// <param name="userID"></param>
        ///// <returns></returns>
        //[HttpGet]
        //public async Task<HeaderResult<List<MenuInfoDto>>> GetUserRightAsync(long userID)
        //{
        //    try
        //    {
        //        var result = await _userApp.GetRoleRightAsync(userID);
        //        HeaderResult<List<MenuInfoDto>> headerResult = new HeaderResult<List<MenuInfoDto>>
        //        {
        //            IsSucceed = result!= null,
        //            //Result = result.,
        //            Message = "获取用户权限成功"
        //        };
        //        return headerResult;
        //    }
        //    catch (Exception ex)
        //    {
        //        HeaderResult<List<MenuInfoDto>> headerResult = new HeaderResult<List<MenuInfoDto>>
        //        {
        //            IsSucceed = false,
        //            Message = "获取用户权限失败" + ex.Message
        //        };
        //        return headerResult;
        //    }
        //}


        /// <summary>
        /// 插入用户角色
        /// </summary>
        /// <param name="userMangerDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<HeaderResult<bool>> InsertUserRoleAsync(List<UserMangerDto> userMangerDto)
        {
            var res = await _userApp.InsertUserRoleAsync(userMangerDto);
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            if (res)
            {
                headerResult.IsSucceed = true;
                headerResult.Message = "插入成功";
            }
            else
            {
                headerResult.IsSucceed = false;
                headerResult.Message = "插入失败";
            }
            return headerResult;
        }

        /// <summary>
        /// 获取菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<HeaderResult<List<MenuInfoDto>>> GetMenuAsync()
        {
            try
            {
                var result = await _userApp.GetMenuAsync();
                HeaderResult<List<MenuInfoDto>> headerResult = new HeaderResult<List<MenuInfoDto>>
                {
                    IsSucceed = result != null,
                    Result = result,
                    Message = "获取菜单成功"
                };
                return headerResult;
            }
            catch (Exception ex)
            {
                HeaderResult<List<MenuInfoDto>> headerResult = new HeaderResult<List<MenuInfoDto>>
                {
                    IsSucceed = false,
                    Message = "获取菜单失败" + ex.Message
                };
                return headerResult;
            }
        }


        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<HeaderResult<bool>> DeleteUserAsync(long userID)
        {
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            var res = await _userApp.DeleteUserAsync(userID);
            if (res)
            {
                headerResult.IsSucceed = true;
                headerResult.Message = "删除成功";
            }
            else
            {
                headerResult.IsSucceed = false;
                headerResult.Message = "删除失败";
            }
            return headerResult;
        }


        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<HeaderResult<bool>> DeleteRoleAsync(long roleID)
        {
            var res = await _userApp.DeleteRoleAsync(roleID);
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            if (res)
            {
                headerResult.IsSucceed = true;
                headerResult.Message = "删除成功";
            }
            else
            {
                headerResult.IsSucceed = false;
                headerResult.Message = "删除失败";
            }
            return headerResult;
        }


        /// <summary>
        /// 更新用户角色
        /// </summary>
        /// <param name="userRoleInfoDtos"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<HeaderResult<bool>> UpdateUserRoleAsync(List<UserMangerDto> userMangerDtos)
        {
            var res = await _userApp.UpdateUserRoleAsync(userMangerDtos);
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            if (res)
            {
                headerResult.IsSucceed = true;
                headerResult.Message = "更新成功";
            }
            else
            {
                headerResult.IsSucceed = false;
                headerResult.Message = "更新失败";
            }
            return headerResult;
        }


        /// <summary>
        /// 更新角色权限
        /// </summary>
        /// <param name="roleRightInfoDtos"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<HeaderResult<bool>> UpdateRoleRightAsync(List<UserMangerDto> userMangerDtos)
        {
            var res = await _userApp.UpdateRoleRightAsync(userMangerDtos);
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            if (res)
            {
                headerResult.IsSucceed = true;
                headerResult.Message = "更新成功";
            }
            else
            {
                headerResult.IsSucceed = false;
                headerResult.Message = "更新失败";
            }
            return headerResult;
        }


        /// <summary>
        /// 插入角色和权限
        /// </summary>
        /// <param name="userMangerDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<HeaderResult<bool>> InsertRoleRightAsync(List<UserMangerDto> userMangerDto)
        {
            var res = await _userApp.InsertRoleRightAsync(userMangerDto);
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            if (res)
            {
                headerResult.IsSucceed = true;
                headerResult.Message = "插入成功";
            }
            else
            {
                headerResult.IsSucceed = false;
                headerResult.Message = "插入失败";
            }
            return headerResult;
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<HeaderResult<bool>> DeleteMenuAsync(long menuID)
        {
            var res = await _userApp.DeleteMenuAsync(menuID);
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            if (res)
            {
                headerResult.IsSucceed = true;
                headerResult.Message = "删除成功";
            }
            else
            {
                headerResult.IsSucceed = false;
                headerResult.Message = "删除失败";
            }
            return headerResult;
        }

        /// <summary>
        /// 获取角色菜单权限（登录及修改用）
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<HeaderResult<List<RoleRightInfoDto>>> GetRoleRightAsync(long userID)
        {
            var res = await _userApp.GetRoleRightAsync(userID);
            HeaderResult<List<RoleRightInfoDto>> headerResult = new HeaderResult<List<RoleRightInfoDto>>();
            if (res != null)
            {
                headerResult.IsSucceed = true;
                headerResult.Result = res;
                headerResult.Message = "查询成功";
            }
            else
            {
                headerResult.IsSucceed = false;
                headerResult.Message = "查询失败";
            }
            return headerResult;
        }


        /// <summary>
        /// 获取角色菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<HeaderResult<List<RoleInfoApiDto>>> GetRoleAsync()
        {
            var res = await _userApp.GetRoleAsync();
            HeaderResult<List<RoleInfoApiDto>> headerResult = new HeaderResult<List<RoleInfoApiDto>>();
            if (res != null)
            {
                headerResult.IsSucceed = true;
                headerResult.Result = res;
                headerResult.Message = "查询成功";
            }
            else
            {
                headerResult.IsSucceed = false;
                headerResult.Message = "查询失败";
            }
            return headerResult;
        }


        /// <summary>
        /// 获取菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<HeaderResult<List<RoleRightInfoDto>>> GetRoleMenuByIDAsync(long roleID)
        {
            HeaderResult<List<RoleRightInfoDto>> headerResult = new HeaderResult<List<RoleRightInfoDto>>();
            try
            {

                var res = await _userApp.GetRoleMenuByIDAsync(roleID);

                if (res != null)
                {
                    headerResult.IsSucceed = true;
                    headerResult.Message = "获取成功";
                    headerResult.Result = res;
                }
                else
                {
                    headerResult.IsSucceed = false;
                    headerResult.Message = "获取失败";
                }
                return headerResult;
            }
            catch (Exception ex)
            {
                if (ex.Message.Length > 50)
                    headerResult.Message = ex.Message.Substring(0, 50);
                else
                    headerResult.Message = ex.Message;
                headerResult.IsSucceed = false;
                return headerResult;
            }

        }


        [HttpGet]
        public async Task<HeaderResult<List<UserRoleInfoDto>>> GetUserRoleInfosAsync(long userID)
        {
            HeaderResult<List<UserRoleInfoDto>> headerResult = new HeaderResult<List<UserRoleInfoDto>>();
            try
            {

                var res = await _userApp.GetUserRoleInfos(userID);

                if (res != null)
                {
                    headerResult.IsSucceed = true;
                    headerResult.Message = "获取成功";
                    headerResult.Result = res;
                }
                else
                {
                    headerResult.IsSucceed = false;
                    headerResult.Message = "获取失败";
                }
                return headerResult;
            }
            catch (Exception ex)
            {
                if (ex.Message.Length > 50)
                    headerResult.Message = ex.Message.Substring(0, 50);
                else
                    headerResult.Message = ex.Message;
                headerResult.IsSucceed = false;
                return headerResult;
            }
        }



        /// <summary>
        /// 获取用户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<HeaderResult<List<UserInfoApiDto>>> GetUserListAsync()
        {
            HeaderResult<List<UserInfoApiDto>> headerResult = new HeaderResult<List<UserInfoApiDto>>();

            var res = await _userApp.GetUserListAsync();
            res.Where(l => l.Password != null).Select(l => l.Password = GenerateMD5.DESDecrypt(l.Password));
            try
            {
                if (res != null)
                {
                    headerResult.IsSucceed = true;
                    headerResult.Message = "获取用户成功";
                    headerResult.Result = res;
                }
                else
                {
                    headerResult.IsSucceed = false;
                    headerResult.Message = "获取用户失败";
                }
                return headerResult;
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.Message;
                return headerResult;
            }
        }

    }
}
